package com.hicampus.portlet.verifycontact;

import java.io.IOException;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import com.hicampus.util.IConstants;
import com.hicampus.util.SMSUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.Contact;
import com.liferay.portal.model.Phone;
import com.liferay.portal.service.PhoneLocalServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.PwdGenerator;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class VerifyContact
 */
public class VerifyContact extends MVCPortlet {
	
	public void saveLocationInfo(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {

		System.out.println("Saving location information");
		
		actionResponse.sendRedirect(ParamUtil.getString(actionRequest, "redirectURL"));
	}

	private GeoLocation getLocation(ActionRequest actionRequest) {
		PortletSession portletSession = actionRequest.getPortletSession();
		
		GeoLocation location = (GeoLocation) portletSession.getAttribute("LIFERAY_SHARED_CURRENT_LOCATION", PortletSession.APPLICATION_SCOPE);
		return location;
	}
	
	public void dialNumber(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {

		
		String contactNumber = ParamUtil.getString(actionRequest, "contactNumber");
		
		int type = 11008;		//ParamUtil.getInteger(actionRequest, "type");
		boolean isPrimary = true;          //ParamUtil.getBoolean(actionRequest, "primary", false);
		GeoLocation location = getLocation(actionRequest);
		
		
		String generatedPIN = PwdGenerator.getPinNumber(); 
		
		PortletSession portletSession = actionRequest.getPortletSession();
		portletSession.setAttribute("GENERATED_PIN", generatedPIN);
		portletSession.setAttribute("Contact_Number", contactNumber);
		portletSession.setAttribute("Type", type);
		portletSession.setAttribute("isPrimary", isPrimary);
		
		int verifyType = ParamUtil.getInteger(actionRequest, "verifyType");
		
		if(!numberExists(contactNumber)){
			if (verifyType == IConstants.VERIFICATION_TYPE_DIAL) {
				//MaxMindUtil.dialNumber(contactNumber, generatedPIN);
				} 
			else {
				String msgText = "Your mobile verification PIN is: " + generatedPIN;
				SMSUtil.push(contactNumber, msgText);
				}
			actionResponse.sendRedirect(ParamUtil.getString(actionRequest, "redirectURL"));
			} 
			else{
				System.out.println("Phone number already exists.");
				actionResponse.setRenderParameter("errorMsg", "DUPLICATE_ENTRY");
				actionResponse.setRenderParameter("jspPage", "/html/verifycontact/step1.jsp");
				}
			}
	
	public void confirmPIN(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {

		PortletSession portletSession = actionRequest.getPortletSession();
		GeoLocation location = (GeoLocation)portletSession.getAttribute("LIFERAY_SHARED_CURRENT_LOCATION", PortletSession.APPLICATION_SCOPE);
		
		ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
		String fourDigitPin = ParamUtil.getString(actionRequest, "fourDigitPin");
		String extension = location.getIdd();
		
		String generatedPIN = (String)portletSession.getAttribute("GENERATED_PIN");
		String contactNumber = (String)portletSession.getAttribute("Contact_Number");
		int type = (Integer)portletSession.getAttribute("Type");
		boolean isPrimary = (Boolean) portletSession.getAttribute("isPrimary");
		
		
		if (fourDigitPin.equals(generatedPIN)) {
			// success
			try {
				Phone ph =PhoneLocalServiceUtil.addPhone(themeDisplay.getUserId(), Contact.class.getName(),themeDisplay.getContact().getPrimaryKey(), contactNumber, extension, type, isPrimary);
			} catch (Exception e) {
				e.printStackTrace();
			}
			actionResponse.sendRedirect(ParamUtil.getString(actionRequest, "redirectURL"));
		}
	}
	
	 public void doView(RenderRequest req, RenderResponse res)
     throws IOException, PortletException{
     
		 String ipAddress = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(req)).getRemoteAddr();

			if (!Validator.isIPAddress(ipAddress)) {
				// give a dummy value
				ipAddress = "122.167.129.104";
			}
 
			PortletSession portletSession = req.getPortletSession();
			GeoLocation location = (GeoLocation)portletSession.getAttribute("LIFERAY_SHARED_CURRENT_LOCATION", PortletSession.APPLICATION_SCOPE);
			
			if (location == null) {
				location = MaxMindUtil.getLocation(ipAddress);
				portletSession.setAttribute("LIFERAY_SHARED_CURRENT_LOCATION", location, PortletSession.APPLICATION_SCOPE);
			}
     super.doView(req, res);
	 }
	 
	 public boolean numberExists(String contactNumber){
        List<Phone> pList = null;
		try {
			pList = PhoneLocalServiceUtil.getPhones();
		} catch (SystemException e) {
			e.printStackTrace();
		}
		
         boolean numberExist = false;
         for(Phone p :pList){
                 if(p.getNumber().equalsIgnoreCase(contactNumber)){
                         numberExist=true;
                         break;
                 }
         }
         
         return numberExist;
	 }


}
